/*
 * CS_SOCIAL_SUMMON_FRIEND.cpp
 *
 *  Created on: 2013. 6. 20.
 *      Author: the2
 */

#include "../../Command.h"
#include "../../User.h"
#include "../../Proxy.h"
#include "../../Config.h"
#include "../../NLogic/Space.h"

namespace ProjectZ { namespace NCommand {

void SendFailRecall(User * pUser, uint32_t receiver_seq, int16_t result)
{
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();

	*pAck << result << receiver_seq << static_cast<uint8_t>(2);

	pAck->MakePacket(CMD_SC_SOCIAL_SUMMON_FRIEND);
	pUser->Send(pAck);
}
/*
// 친구 소환
// A->SERVER: CS_SOCIAL_SUMMON_FRIEND
// SERVER->B: SC_SOCIAL_SUMMON_FROM_FRIEND
// B->SERVER: CS_SOCIAL_SUMMON_FROM_FRIEND
// SERVER->A: SC_SOCIAL_SUMMON_FRIEND
CS_SOCIAL_SUMMON_FRIEND				0x2128
{
	u32 u32gid;						// 상대방 GID
}

SC_SOCIAL_SUMMON_FROM_FRIEND		0x2139
{
	u32 u32gid;						// 상대방 GID
	u32	u32ip;						// channel ip
	u16	u16port;					// channel port
	u16 u16server_id;				// 접속하는 서버 인덱스
	u16	u16channel_id;				// 접속하는 채널 인덱스
}
*/
void Command::CS_SOCIAL_SUMMON_FRIEND(User * pUser, ::Network::Packet * pPacket)
{
	PREPARE_PACKET;

	uint32_t receiver_seq;	// 상대방 GID

	*pPacket >> receiver_seq;

	User * pReceiver = NProxy::GetUser(receiver_seq);

	if (NULL == pReceiver)
	{
		SendFailRecall(pUser, receiver_seq, ACK_OK);
		return;
	}

	NLogic::Space * pReceiverSpace = pReceiver->GetSpace();
	if(NULL == pReceiverSpace)
	{
		SendFailRecall(pUser, receiver_seq, ACK_OK);
		return;
	}

	if(User::EnumState::__NONE__ != pReceiver->GetEnumState() && NLogic::Space::SpaceType::WORLD != pReceiverSpace->GetData()._spaceType)
	{
		SendFailRecall(pUser, receiver_seq, ACK_OK);
		return;
	}

	NLogic::Space * pSpace = pUser->GetSpace();
	if(NULL == pSpace)
	{
		SendFailRecall(pUser, receiver_seq, ACK_OK);
		return;
	}

	if(Config::InstancePtr()->GetResourceDatabase()._maxChannelUser <= pSpace->GetCCU())
	{
		SendFailRecall(pUser, receiver_seq, ACK_CHANNEL_USER_FULL);
		return;
	}

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << pUser->GetUserSeq()
//			<< inet_addr(::ProjectZ::Config::Instance().GetResourceFile()._myIP.c_str())
//			<< static_cast<uint16_t>(::ProjectZ::Config::Instance().GetResourceDatabase()._port)
			<< static_cast<uint16_t>(::ProjectZ::Config::Instance().GetResourceDatabase()._channelUniqueIndex)
			<< static_cast<uint16_t>(pUser->GetSpace()->GetData()._index);

	pAck->MakePacket(CMD_SC_SOCIAL_SUMMON_FROM_FRIEND);
	pReceiver->Send(pAck);

	pUser->GetSpace()->ReserveChannel(pReceiver);
//	::ProjectZ::NProxy::Recall(pUser, receiver_seq);
}

} /*NCommand*/ } /*ProjectZ*/


